<template>
	<!--订单详情-->
	<view class="page-content">
		<view class="page-top">
			<!--订单详情信息-->
			<order-top-info :obj="useraddress" @todetail="todetail"></order-top-info>
			<!--橱窗-->
			<block v-for="(item,index) in objdata.objlist" :key="index">
				<view class="page-top-box">
					<product-number :obj="item" :pageType="pageType" :is_show_price="false"></product-number>
				</view>
			</block>
			
		</view>
		<view class="page-body">
			<!--流程-->
			<view class="page-body-title">批注</view>
			<view class="input-row2">
				<textarea class="input-row-w" v-model="mark" placeholder-style="color:#ccc" placeholder="请输入内容" />
			</view>
		</view>
		<view class="page-foot">
			<view class="page-foot-l" @click="toAdd(objdata.flow_path_id)">
				<view class="box-foot-l" v-if="objdata3.uid!=''">
					<image class="box-foot-l-img" :src="objdata3.img || $mAssetsPath.icon_mrtx"></image>
				</view>
				<view class="box-foot-l" v-else>
					<view class="box-foot-l-b">+</view>
				</view>
			</view>
			<view class="page-foot-r">
				<view class="page-foot-r-t" @click="toPast">确认下单</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapGetters,
		mapActions
	} from "vuex";
	import {
		createOrders, //创建订单
		getAddressLists, //地址
		orderInfos, //获取再下一单数据
	} from '@/common/api.js';
	import orderTopInfo from '@/pages/components/order_top_info.vue'; //地址
	import productNumber from '@/pages/components/product_number.vue'; //产品信息+橱窗
	export default{
		components: {
			orderTopInfo,productNumber
		},
		data() {
			return {
				unfold:false, //是否查看更多
				objdata3:{
					uid:'',
					img:'',
				},
				useraddress:{
					id:'',
					name: '张三',
					mobile: '1347627347',
					address: '福建省厦门市湖里区吕玲路江头街道大儒世家119号',
				},
				objdata:{
					mark:'',
					objlist:[],
					list:[]
				},
				objdata2:{
					mark:'',
					objlist:[],
					list:[]
				},
				fromdata: {
					company_id: '',
					type: '2',
					page: 1,
					limit: 99
				},
				params:{
					company_id:'',
					task_id:'',
					role_id:'', //下单权限
					mark:'',
					address_id:'',
					to_user_id:'',
					task_data:[]
				},
				pageType:'',
				mark:'',
				order_id:'',
				flow_path_id:'',
				jsondata:{},
			}
		},
		computed: {
			...mapGetters(['companyInfo', 'userInfo', 'address']),
		},
		onShow() {
			
		},
		onLoad(option) {
			console.log(222,option)
			this.fromdata.company_id = this.companyInfo.id;
			this.params.company_id = this.companyInfo.id;
			if(option.objdata){
				this.objdata2 = JSON.parse(option.objdata);
			}
			if(option.pageType){
				this.pageType = option.pageType;
				this.getdata();
			}
			if(option.flow_path_id){
				this.flow_path_id = option.flow_path_id;
			}
			if(option.id){
				this.order_id = option.id;
				this.getdata2(); //获取再下一单的数据
			}
			this.getList();
		},
		methods:{
			...mapActions('user',['setAddress',]),
			resdata(){
				//this.getdata();
				if(!!this.address){
					this.useraddress = this.address;
				}
			},
			//返回指派人信息
			resdata2(item){
				console.log(2222,item)
				this.objdata3 = item;
			},
			//获取地址
			getdata(){
				getAddressLists(this.fromdata).then(res=>{
					if(res.code === 2000){
						if(res.data.data && res.data.data.length>0){
							res.data.data.forEach(item=>{
								if(item.order === 1){
									this.useraddress = item
									this.setAddress(item)
									console.log(333,this.useraddress)
								}
							})
						}
					}
				})
			},
			//获取再下一单的数据
			getdata2(){
				orderInfos({order_id:this.order_id}).then(res=>{
					if(res.code === 2000){
						const {address,order_flow,order_list,jsondata} = res.data;
						this.useraddress = address;
						this.jsondata = JSON.parse(jsondata);
						console.log(1234,this.jsondata)
						let objlist = []
						if(order_list && order_list.length>0){
							order_list.forEach(item=>{
								let datalist = [];
								let textures = [];
								this.$set(item, 'datalist', datalist)
								this.$set(item, 'textures', textures)
								this.$set(item, 'img_name', item.img_name)
								if(item.data && item.data.length>0){
									item.data.forEach(item2=>{
										console.log(1111,item2)
										if(item2.width === '0.00'){
											item2.width = ''
										}
										if(item2.height === '0.00'){
											item2.height = ''
										}
										if(item2.coding_name !== '定制'){
											if (item2.top_id === 0) {
												item.datalist.push(item2)
												let data = [];
												let texture = [];
												this.$set(item2, 'data', data)
												this.$set(item2, 'texture', texture)
												if(item.datalist && item.datalist.length>0){
													item.datalist.forEach(item3=>{
														if (item2.top_id === item3.product_coding_id) {
															item3.data.push(item2)
															item3.texture.push(item2.texture_name)
														}
													})
												}
											}
										}else if(item2.coding_name === '定制'){
											item.textures.push(item2)
										}
										
									})
								}
								if(item.data && item.data.length>0){
									item.data.forEach(item2=>{
										if(item.datalist && item.datalist.length>0){
											item.datalist.forEach(item3=>{
												if (item2.top_id === item3.product_coding_id) {
													item3.data.push(item2)
													item3.texture.push(item2.texture_name)
												}
												if(item3.data && item3.data.length>0){
													item3.data.forEach(item4=>{
														let data = [];
														this.$set(item4, 'data', data)
													})
												}
											})
										}
									})
								}
								if(item.data && item.data.length>0){
									item.data.forEach(item2=>{
										if(item.datalist && item.datalist.length>0){
											item.datalist.forEach(item3=>{
												if(item3.data && item3.data.length>0){
													item3.data.forEach(item4=>{
														if (item2.top_id === item4.product_coding_id) {
															item4.data.push(item2);
															item3.texture.push(item2.texture_name) //增加材质
														}
													})
												}
											})
										}
									})
								}
								objlist.push(item);
							})
						}
						this.objdata.objlist = objlist;
						this.$forceUpdate() //二维数组，开启强制渲染
					}
				})
			},
			//跳转地址列表
			todetail(){
				uni.navigateTo({
					url:'/pages/address/address'
				})
			},
			//调整指派人员页面
			toAdd(id){
				if(this.order_id === ''){
					uni.navigateTo({
						url: `/pages/designate/des_one?flow_path_id=${id}&type=c`
					})
				}else{
					uni.navigateTo({
						url: `/pages/designate/des_one?flow_path_id=${this.flow_path_id}&type=c`
					})
				}
				
			},
			//通过
			toPast(){
				console.log(999, this.objdata)
				if(this.useraddress.id === ''){
					uni.showToast({
						title:'请选择地址！',
						icon:'none',
						duration:2000
					})
					return
				}
				if(this.objdata3.uid === ''){
					uni.showToast({
						title:'请增加指派的人员！',
						icon:'none',
						duration:2000
					})
					return
				}
				
				this.params.mark = this.mark; //批注
				this.params.address_id = this.useraddress.id; // 地址
				this.params.to_user_id = this.objdata3.uid; // 指派人员
				
				if(!!this.jsondata.task_id){
					this.params.task_id = this.jsondata.task_id; //任务id
					this.params.task_data = this.jsondata.task_data;
				}else{
					this.params.task_id = this.objdata.id; //任务id
					let task_datas = [];
					if (this.objdata.objlist && this.objdata.objlist.length > 0) {
						this.objdata.objlist.forEach(item => {
							if(this.pageType === 'pop'){
								let task_data = {
									task_album_join_id: '',
									product_album_data_id: '',
									data: []
								}
								task_data.task_album_join_id = item.id;
								task_data.product_album_data_id = item.product_album_data_id;
								if (item.datalist && item.datalist.length > 0) {
									item.datalist.forEach(item2 => {
										task_data.data.push(item2)
									})
								}
								if (item.datalist && item.datalist.length > 0) {
									item.datalist.forEach(item2 => {
										if (item2.data && item2.data.length > 0) {
											item2.data.forEach(item3 => {
												task_data.data.push(item3)
											})
										}
									})
								}
								if (item.datalist && item.datalist.length > 0) {
									item.datalist.forEach(item2 => {
										if (item2.data && item2.data.length > 0) {
											item2.data.forEach(item3 => {
												if (item3.data && item3.data.length > 0) {
													item3.data.forEach(item4 => {
														task_data.data.push(item4)
													})
												}
											})
										}
									})
								}
								if (item.textures && item.textures.length > 0) {
									item.textures.forEach(item2 => {
										task_data.data.push(item2)
									})
								}
								task_datas.push(task_data)
							}else if(this.pageType === 'prop'){
								let task_data = {
									task_album_join_id: '',
									image_name:'',
									url: '',
									mark:'',
									data: []
								}
								task_data.task_album_join_id = item.id;
								task_data.url = item.url;
								task_data.image_name = item.image_name;
								task_data.mark = item.mark;
								if (item.datalist && item.datalist.length > 0) {
									item.datalist.forEach(item2 => {
										task_data.data.push(item2)
									})
								}
								if (item.datalist && item.datalist.length > 0) {
									item.datalist.forEach(item2 => {
										if (item2.data && item2.data.length > 0) {
											item2.data.forEach(item3 => {
												task_data.data.push(item3)
											})
										}
									})
								}
								task_datas.push(task_data)
							}
						})
					}
					
					if (task_datas && task_datas.length > 0) {
						task_datas.forEach(item => {
							if (item.data && item.data.length > 0) {
								item.data.forEach(item2 => {
									delete item2.data;
									delete item2.create_time;
									delete item2.company_id;
									delete item2.delete_time;
									//delete item2.name;
									//delete item2.next_num;
									delete item2.product_texture_id;
									//delete item2.texture;
									delete item2.type;
									delete item2.update_time;
									if(item2.has_next === 1){ //清楚一级和二级（有三级的）存在数量的 number
										delete item2.number;
									}
								})
							}
						})
					}
					this.params.task_data = task_datas
				}
				
				console.log(999, this.params)
				//return false
				createOrders(this.params).then(res => {
					if (res.code === 2000) {
						uni.showToast({
							title: '下单成功！',
							icon: 'none',
							duration: 2000
						})
						setTimeout(() => {
							uni.navigateTo({
								url:`/pages/work/work`
							})
						}, 500)
					}else{
						uni.showToast({
							title:res.msg,
							icon:"none",
							duration: 2000
						})
					}
				}) 
			},
			getList(){
				console.log(7777, this.objdata2)
				this.objdata = this.objdata2;
				//清除数量为零的图片 或者 编码
				if (this.objdata2.objlist && this.objdata2.objlist.length > 0) {
				   this. objdata2.objlist.forEach((item, index) => {  //item-图片
				        this.$set(item, 'num', 0);
				        if (item.datalist && item.datalist.length > 0) {
				            item.datalist.forEach((item2, index2) => { //item2-秋季外套
				                this.$set(item2, 'has_next', item2.data.length > 0 ? 1 : 0);
				                if (item2.has_next === 1 && item2.data && item2.data.length > 0) {
				                    item2.data.forEach((item3, index3) => { //item3-裤子
				                        this.$set(item3, 'has_next', item3.data.length > 0 ? 1 : 0);
				                        if (item3.has_next === 1 && item3.data && item3.data.length > 0) {
				                            item3.data.forEach((item4, index4) => { //item4子菜单
				                                item2.number += parseInt(item4.number)
				                                item.num += parseInt(item4.number)
				                            })
				                        } else if (item3.has_next === 0) {
				                            item2.number += parseInt(item3.number)
				                            item.num += parseInt(item3.number)
				                        }
				                    })
				                }
				            })
				        }
						if(item.textures && item.textures.length>0){
							item.textures.forEach(ite=>{
								item.num += parseInt(ite.number)
							})
						}
				    })
				}
				console.log(888, this.objdata2.objlist)
				
				let ii = [];
				let list = this.objdata2.objlist;
				if (this.objdata2.objlist && this.objdata2.objlist.length > 0) {
				    this.objdata2.objlist.forEach((item, index) => {  //item-图片
				        if (item.num > 0) {
				            let iii = []
				            let list2 = item.datalist;
				            if (item.datalist && item.datalist.length > 0) {
				                item.datalist.forEach((item2, index2) => { //item2-秋季外套
				                    if (item2.number > 0) {
				                        let iiii = []
				                        let list3 = item2.data;
				                        if (item2.has_next === 1 && item2.data && item2.data.length > 0) {
				                            item2.data.forEach((item3, index3) => { //item3-裤子
				                                if (item3.number === 0 && item3.has_next === 1) {
				                                    let iiiii = [];
				                                    let list4 = item3.data;
				                                    if (item3.has_next === 1 && item3.data && item3.data.length > 0) {
				                                        item3.data.forEach((item4, index4) => { //item4子菜单
				                                            if (item4.number > 0) {
				
				                                            } else {
				                                                iiiii.push(index4)
				                                            }
				                                        })
				                                    }
				                                    if (iiiii && iiiii.length > 0) {
				                                        iiiii.forEach(ite => {
				                                            delete list4[ite]
				                                        })
				                                    }
				                                    item3.data = list4.filter(Boolean);
				
				                                } else if (item3.number === 0 && item3.has_next === 0) {
				                                    iiii.push(index3)
				                                }
				                            })
				                        }
				                        if (iiii && iiii.length > 0) {
				                            iiii.forEach(ite => {
				                                delete list3[ite]
				                            })
				                        }
				                        item2.data = list3.filter(Boolean);
										
				                    } else {
				                        iii.push(index2)
				                    }
				                    if (iii && iii.length > 0) {
				                        iii.forEach(ite => {
				                            delete list2[ite]
				                        })
				                    }
				                    item.datalist = list2.filter(Boolean);
				                })
				            }
							console.log(444,item.textures)
							let aaa = []
							let list0 = item.textures;
							if(item.textures && item.textures.length>0){
								item.textures.forEach((ite,a)=>{
									if(parseInt(ite.number) > 0){
										
									}else{
										aaa.push(a)
									}
								})
							}
							if (aaa && aaa.length > 0) {
							    aaa.forEach(b => {
							        delete list0[b]
							    })
							}
							item.textures = list0.filter(Boolean);
							
				        } else {
				            ii.push(index)
				        }
				    })
				}
				if (ii && ii.length > 0) {
				    ii.forEach(ite => {
				        delete list[ite]
				    })
				}
				this.objdata.objlist = list.filter(Boolean);
				
				
				if(this.objdata.objlist && this.objdata.objlist.length>0){
					this.objdata.objlist.forEach(item=>{
						if(item.datalist && item.datalist.length>0){
							item.datalist.forEach(item2=>{
								let ii = []
								let list = item2.data;
								let texture = []
								if(item2.data && item2.data.length>0){
									item2.data.forEach((item3,index)=>{
										if(item3.has_next === 0){
											if(item3.number === 0){
												ii.push(index)
											}else{
												texture.push(item3.texture_name)
											}
										}else if(item3.has_next === 1 && item3.data.length>0){
											let iii = []
											let list2 = item3.data;
											if(item3.data && item3.data.length>0){
												item3.data.forEach((item4,ind)=>{
													if(item4.number === 0){
													   iii.push(ind)
													}else{
														texture.push(item4.texture_name)
													}
												})
											}
											if(iii && iii.length>0){
												iii.forEach(ite=>{
													delete list2[ite]
												})
											}
											item3.data = list2.filter(Boolean)
										}
										if(item3.has_next === 1 && item3.data.length===0){
											ii.push(index)
										}
									})
								}
								if(ii && ii.length>0){
									ii.forEach(ite=>{
										delete list[ite]
									})
								}
								item2.data = list.filter(Boolean);
								item2.texture = texture; //材质重选
							})
						}
					})
				}
				console.log(444,this.objdata.objlist)
			},
			checkmore(){
				this.unfold = !this.unfold
				this.getList()
			}
		}
	}
</script>

<style lang="scss">
	.page-content{
		display: flex;
		flex-direction: column;
		.page-foot{
			display: flex;
			position: fixed;
			bottom: 0;
			left: 0;
			right: 0;
			height: 100rpx;
			background: #f5f5f5;
			.page-foot-l{
				flex: 3;
				display: flex;
				align-items: center;
				padding-left: 30rpx;
				.box-foot-l{
					display: flex;
					align-items: center;
					.box-foot-l-b{
						width: 46rpx;
						height: 46rpx;
						background: #198FFF;
						border-radius: 4rpx;
						color: #fff;
						line-height: 44rpx;
						text-align: center;
					}
					.box-foot-l-img{
						width: 46rpx;
						height: 46rpx;
						bborder-radius: 4rpx;
						margin-right: 14rpx;
					}
				}
			}
			.page-foot-r{
				flex: 1;
				display: flex;
				justify-content: center;
				align-items: center;
				font-size: 36rpx;
				line-height: 100rpx;
				font-weight: bold;
				color:$font-color-w;
				text-align: center;
				
				.page-foot-r-t{
					flex: 1;
					background: #198FFF;
				}
			}
		}
		.page-top{
			display: flex;
			flex-direction: column;
			.page-top-box{
				border-bottom: 20rpx solid #f5f5f5;
			}
			.loadMore{
			    display: flex;
			    justify-content: center;
			    color: $font-color-3;
			    font-size: 26rpx;
				height: 60rpx;
				line-height: 60rpx;
				background: $uni-bg-color-grey;
			    .sel-input-icon{
			        margin-left: 8rpx;
			        font-size: 26rpx;
			        transform:rotate(-90deg);
			        -ms-transform:rotate(-90deg); 	/* IE 9 */
			        -moz-transform:rotate(-90deg); 	/* Firefox */
			        -webkit-transform:rotate(-90deg); /* Safari 和 Chrome */
			        -o-transform:rotate(-90deg);
			    }
			    .sel-input-icon2{
			        margin-left: 8rpx;
			        font-size: 26rpx;
			        transform:rotate(90deg);
			        -ms-transform:rotate(90deg); 	/* IE 9 */
			        -moz-transform:rotate(90deg); 	/* Firefox */
			        -webkit-transform:rotate(90deg); /* Safari 和 Chrome */
			        -o-transform:rotate(90deg);
			    }
			}
		}
		.page-body{
			display: flex;
			flex-direction: column;
			padding: 40rpx 30rpx;
			margin-bottom: 50rpx;
			.page-body-title{
				font-size: 32rpx;
				font-weight: 400;
				height: 36rpx;
				line-height: 36rpx;
				color: $font-color-3;
				border-left: 6rpx solid #198FFF;
				color: $font-color-3;
				padding-left: 20rpx;
			}
			.input-row2 {
				margin: 30rpx 0 50rpx 0;
				padding: 10rpx 20rpx;
				height: 340rpx;
				display: flex;
				width: 100%;
				flex-direction: row;
				border: 1rpx solid #198FFF;
				border-radius: 10rpx;
			
				.input-row-w {
					display: flex;
					height: 320rpx;
					flex: 1;
					width: 100%;
					color: $font-color-3;
					font-size: 32rpx;
				}
			}
		}
	}
</style>
